var Promise = require('es6-promise').Promise;
// var isMobile = require('ismobilejs');

function matchTheme(req, res) {
    // if (isMobile(req.headers['user-agent']).any) {
    //     res.locals.theme = "single-page";
    //     res.locals.index = "dist/index";
    // } else {
    res.locals.theme = "default";
    // }
    res.templet({});
}
module.exports = {
    index: function(req, res, next) {
        // 获取产品列表
        Product.find({
            sort: "listorder ASC",
            limit: 4
        }).then(function(products) {
            res.locals.products = products;

            return Fragment.find({
                varname: "home_intro"
            });
        }).then(function(fragmentes) {
            if (fragmentes) {
                fragmentes.forEach(function(one, key) {
                    res.locals[one.varname] = one.content;
                });
            }

            return Slide.find();
        }).then(function(slides) {
            res.locals.slides = slides;

            res.locals.currentMenu = "home";
            res.locals.view = "index";
            //     return matchTheme(req,res);
            // }, function reject(err) {
            //     return next(err);

        })

        //获取导航信息
        .then(() => Category.find({
            isnavigation: 1
        })).then(function(root_categorys) {
            return res.locals.root_categorys = root_categorys;
        })

        // 获取健康宣讲
        .then((root_categorys) => Article.find({
            where: {
                category: root_categorys.find((category) => category.dir === "speak").id
            },
            sort: "listorder ASC",
            limit: 3,
        })).then(articles => {
            res.locals.speak_articles = articles;
            return res.locals.root_categorys
        })

        //获取奥索动态
        .then((root_categorys) => Article.find({
            where: {
                category: root_categorys.find((category) => category.dir === "news").id
            },
            sort: "listorder ASC",
            limit: 4,
        })).then(news => {
            res.locals.news = news;
            return res.locals.root_categorys
        })

        .then(() => matchTheme(req, res)).catch(next);
    },

    /**
     * 列表页面 
     * @param  {[category]}  目录
     */
    list: function(req, res, next) {
        var category = req.param("category").trim();
        var model_str = "category";
        var model = null;

        var current_category = null;
        Category.find({
                isnavigation: 1
            }).then(function(root_categorys) {
                res.locals.root_categorys = root_categorys;
            }).then(function() {
                if (category) {
                    return Category.findOne({
                        dir: category
                    });
                } else {
                    return new Promise(function(resolve, reject) {
                        return resolve(null);
                    });
                }
            })
            .then(function(category) {
                current_category = category;
                return Category.getTree({
                    model: category.model
                });
            })
            .then(function(getTree) {
                res.locals.categorys = getTree;

                var current_page = req.query['p'] || req.query['page'] || 1;
                var pageContion = {
                    sort: "listorder ASC",
                    limit: current_category.pagesize ? current_category.pagesize : 10
                };

                if (current_category) {
                    pageContion.where = {
                        category: current_category.id
                    };
                }

                model_str = Util.firstUper(current_category.model);
                model = eval("model = " + model_str);

                return Pagination(model, {
                    current_page: current_page
                }, pageContion);
            })
            .then(function(rs) {
                res.locals.data = rs.data;
                res.locals.paging = rs.paging;

                // 获取热门数据
                return model.find({
                    limit: 5
                });
            })
            .then(function(hot_records) {
                res.locals.hot_records = hot_records;

                res.locals.category = current_category;
                res.locals.currentMenu = category;
                res.locals.view = current_category.tpllist ? current_category.tpllist : current_category.model + "_list";

                return matchTheme(req, res);
            }, function(err) {
                return next(err);
            });
    },

    /**
     * 详情页 
     * @param  {[category]}  目录
     * @param  {[id]}  ID
     */
    detail: function(req, res, next) {
        var category = req.param("category").trim();
        var id = req.param("id").trim();
        var model_str = "category";
        var data = null;
        var model;

        var current_category = null;
        Category.find({
            isnavigation: 1
        })

        .then(function(root_categorys) {
            res.locals.root_categorys = root_categorys;
        })

        .then(function() {
            if (category) {
                return Category.findOne({
                    dir: category
                });
            } else {
                return new Promise(function(resolve, reject) {
                    return resolve(null);
                });
            }
        })


        .then(function(category) {
            current_category = category;
            return Category.getTree({
                model: category.model
            });
        })


        .then(function(getTree) {
            res.locals.categorys = getTree;

            model_str = Util.firstUper(current_category.model);
            model = eval("model = " + model_str);

            return model.findOne({
                id: id
            });
        })

        .then(function(rs) {
            data = rs;
            res.locals.data = rs;
            res.locals[current_category.model] = rs;

            // 获取热门数据
            return model.find({
                limit: 5
            });
        })

        .then(function(hot_records) {
            res.locals.hot_records = hot_records;

            res.locals.category = current_category;
            res.locals.currentMenu = category;
            res.locals.view = current_category.tpldetail ? category.tpldetail : current_category.model + "_detail";
            if (data.tpl) {
                res.locals.view = data.tpl;
            }
            return matchTheme(req, res);
        }, function(err) {
            return next(err);
        });
    }
};